package com.threeox.commonlibrary.util;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

import com.threeox.utillibrary.util.ActivityHelper;
import com.threeox.utillibrary.util.Basic2ObjUtil;
import com.threeox.utillibrary.util.LogUtils;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @ClassName: FragmentUtils
 * 
 * @Description: TODO()
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/10 14:49
 * 
 * @version 1.0
 * 
 */
public class FragmentUtils {

    private static final String TAG = FragmentUtils.class.getSimpleName();

	private Fragment mFragment;
	private Bundle mBundle = null;
    private Class<? extends Object> mBundelClass;
	private static FragmentUtils mFragmentUtils;

    private FragmentUtils(){}

	private FragmentUtils(Class cls) {
		try {
            this.mBundle = new Bundle();
            this.mBundelClass = mBundle.getClass();
            this.mFragment = (Fragment) cls.newInstance();
		} catch (Exception e) {
			LogUtils.e(TAG, "FragmentUtils:报错了:" + e.getMessage());
		}
	}

	public static FragmentUtils init(Class cls) {
		mFragmentUtils = new FragmentUtils(cls);
		return mFragmentUtils;
	}

    /**
     *
     * @param key
     * @param value
     * @return
     */
	public FragmentUtils putBundle(String key, Object value) {
		put(mBundelClass, mBundle, key, value);
		return mFragmentUtils;
	}

    /**
     * 传入Bundle根据Map
     *
     * @param data
     * @return
     */
    public FragmentUtils putBundle(Map<String,Object> data) {
        if (data != null) {
            Set<String> set = data.keySet();
            for (String key : set) {
                putBundle(key, data.get(key));
            }
        }
        return this;
    }

	public <T extends Fragment> T start() {
		mFragment.setArguments(mBundle);
		return (T) mFragment;
	}

	private void put(Class cls, Object obj, String key, Object value) {
		Class<? extends Object> clazz = null;
		try {
			clazz = Basic2ObjUtil.getInstance().getBasicClass(value.getClass());
			injectMethod(cls, obj, key, value, clazz);
		} catch (Exception e) {
			try {
				if (value instanceof Serializable) {
					clazz = Serializable.class;
				} else if (value instanceof Parcelable) {
					clazz = Parcelable.class;
				} else if (value instanceof Parcelable[]) {
					clazz = Parcelable[].class;
				}
				injectMethod(cls, obj, key, value, clazz);
			} catch (Exception e1) {
				LogUtils.e(TAG, "注入数据失败:" + e1.getMessage());
			}
		}
	}

	private void injectMethod(Class cls, Object obj, String key, Object value, Class<? extends Object> _Cls) throws Exception {
		LogUtils.i(TAG, "转换之后的Cls:" + _Cls.getName());
		Method[] methods = cls.getMethods();
		for (Method method : methods) {
			// 说明已put开头
			if (method.getName().indexOf("put") == 0) {
				try {
					method.invoke(obj, key, value);
					LogUtils.i(TAG, "注入数据成功:" + _Cls.getName() + "-->" + value);
				} catch (Exception e) {
					LogUtils.e(TAG, method.getName() + "方法注入数据失败:" + _Cls.getName() + "-->" + value);
				}
			}
		}
	}
}
